package com.v;

import com.Dao.UserDao;
import com.entity.User;
import com.v.login.Interface;

import javax.swing.*;
import java.awt.*;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class Index extends JFrame {
    private JPanel panel;
    private JLabel toplable, label, username, sex, pwd, repwd, prompt;
    private JTextField usernameFid;
    private JPasswordField pwdFid, repwdFid;
    private JRadioButton mRbtn, wRbtn;
    private ButtonGroup buttonGroup;
    private ImageIcon icon = new ImageIcon(Index.class.getResource("1.jpg"));
    private JButton back, confirm;
    private Font font = new Font("宋体", Font.BOLD, 13);
    private boolean flag;


    private int i;

    public Index index;






    public Index() throws HeadlessException {
        setTitle("注册账号");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1086, 755);
        setLocationRelativeTo(null);
        setResizable(false);

        panel = (JPanel) getContentPane();
        panel.setOpaque(false);//将面板设置为透明
        label = new JLabel(icon);
        getLayeredPane().add(label, Integer.valueOf(Integer.MIN_VALUE));
        label.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
        panel.setLayout(null);


        toplable=new JLabel("电  影  购  票  系  统");
        toplable.setBounds(380,40,300,45);
        toplable.setFont(new Font("微软雅黑",Font.BOLD,22));
        toplable.setForeground(Color.black);
        panel.add(toplable);

        //用户名
        username = new JLabel("用户名:");
        username.setBounds(120, 100, 50, 28);
        username.setFont(font);
        username.setForeground(new Color(196, 255, 14));
        panel.add(username);



        usernameFid = new JTextField();
        usernameFid.setBounds(172, 102, 150, 23);
        usernameFid.setHorizontalAlignment(SwingConstants.CENTER);
        usernameFid.setFont(new Font("楷体", Font.BOLD, 13));
        panel.add(usernameFid);



        //密码
        pwd = new JLabel("密  码:");
        pwd.setBounds(120, 160, 70, 28);
        pwd.setForeground(new Color(196, 255, 14));
        pwd.setFont(font);

        pwdFid = new JPasswordField();
        pwdFid.setBounds(172, 162, 150, 23);
        panel.add(pwd);
        panel.add(pwdFid);


        mRbtn = new JRadioButton("用户", true);
        mRbtn.setBounds(150, 220, 80, 20);
        mRbtn.setBackground(Color.white);
        //选择点击事件
        mRbtn.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange()==ItemEvent.SELECTED){
                    i=0;
                }
            }
        });

        wRbtn = new JRadioButton("管理员");
        wRbtn.setBounds(248, 220, 80, 20);
        wRbtn.setBackground(Color.white);
        //选择点击事件
        wRbtn.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if(e.getStateChange()==ItemEvent.SELECTED){
                    i=1;
                }
            }
        });


        buttonGroup = new ButtonGroup();
        buttonGroup.add(mRbtn);
        buttonGroup.add(wRbtn);

        panel.add(mRbtn);
        panel.add(wRbtn);


        //按钮
        confirm = new JButton("注册");
        confirm.setBackground(Color.BLUE);
        confirm.setForeground(Color.YELLOW);
        confirm.setFocusPainted(false);
        confirm.setFont(font);
        confirm.setBounds(153, 278, 75, 32);
        panel.add(confirm);
        confirm.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                  new RegisterFrame();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(new Cursor(12));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(new Cursor(0));
            }
        });

        back = new JButton("登录");
        back.setBackground(Color.BLUE);
        back.setFocusPainted(false);
        back.setForeground(Color.YELLOW);
        back.setFont(font);
        back.setBounds(255, 278, 75, 32);
        panel.add(back);
        //鼠标点击事件
        back.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                User user=new User();
                UserDao userDao = new UserDao();
                  //调用方法查询数据库中的昵称和密码
                    List<User> users = userDao.queryUsersById(usernameFid.getText(), pwdFid.getText());
                    for (User user1 : users) {
                        System.out.println(users);
                        user.setuName(usernameFid.getText());
                        user.setuPass(pwdFid.getText());
                        user.setuType(user1.getuType());
                        user.setUid(user1.getUid());
                    }

                //判断文本域中输入的值是否与数据库中的值是否相同
                if(usernameFid.getText().equals(user.getuName()) &&
                        pwdFid.getText().equals(user.getuPass()) &&
                      i==user.getuType()   ){


                    new Interface(usernameFid);
                    dispose();


                }
                else {
                    System.out.println("登录失败");
                }



            }

            @Override
            public void mouseEntered(MouseEvent e) {
                setCursor(new Cursor(12));

            }

            @Override
            public void mouseExited(MouseEvent e) {
                setCursor(new Cursor(0));

            }

        });
        setVisible(true);

    }

    public static void main(String[] args) {

        new Index();
    }

}
